library(network)
library(ggnetwork)
library(igraph)
library(intergraph)
library(tidyverse)
library(data.table)
library(dtplyr)

source("pundits_functions.R")

set.seed(11111)

load("../output/aggregate_nets/pundits_parrot_net_all.RData")
load("../output/aggregate_nets/pundits_parrot_net_noparty.RData")
load("../output/aggregate_nets/pundits_parrot_net_symbolic.RData")
load("../output/aggregate_nets/pundits_parrot_net_operational.RData")
load("../output/aggregate_nets/pundits_parrot_net_nonpolicy.RData")

topic_refdf <- data.frame(formal_names = c("China","Class","Climate",
                                           "Conservative","Democrat",
                                           "Far Left","Far Right",
                                           "Gender","Guns","Health Care/Insurance",
                                           "Immigration","Iran","Israel","LGBT",
                                           "Liberal","Mueller","Progressive",
                                           "Race","Reproductive Health","Republican",
                                           "Taxes and Spending",
                                           "Trade"),
                          topic = c('china','class','climate','conservative','democrat',
                                    'far_left','far_right','gender','guns',
                                    'health_care_insurance','immigration',
                                    'iran','israel','lgbt','liberal',
                                    'mueller','progressive','race',
                                    'reproductive_health','republican',
                                    'taxes_spending','trade'))

net_prep_plot <- function(net){
  g <- igraph::graph_from_adjacency_matrix(net$sample$graph,
                                           weighted = TRUE,
                                           mode = "undirected")
  
  V(g)$issue <- sapply(V(g)$name, function(v){
    s <- strsplit(v, split = "_")[[1]]
    return(paste0(s[2:length(s)], collapse = "_"))
  })
  V(g)$degree<-igraph::degree(g,mode="all")
  
  graph_df <- as_long_data_frame(g) %>%
    mutate(issue_match = as.numeric(from_issue == to_issue))
  
  gdat <- igraph::as_data_frame(g, what = "both")
  
  gdat$edges$from_issue <- sapply(gdat$edges$from, function(f){
    s <- strsplit(f, split = "_")[[1]]
    return(return(paste0(s[2:length(s)], collapse = "_")))
  })
  gdat$edges$to_issue <- sapply(gdat$edges$to, function(f){
    s <- strsplit(f, split = "_")[[1]]
    return(paste0(s[2:length(s)], collapse = "_"))
  })
  gdat$edges$issue_match <- as.numeric(gdat$edges$from_issue == gdat$edges$to_issue)
  gdat$edges <- gdat$edges %>%
    left_join(gdat$vertices %>% dplyr::select(name, degree),
              by = c("from" = "name"))
  gback <- igraph::graph_from_data_frame(gdat$edges)
  
  gg <- ggnetwork(intergraph::asNetwork(gback))
  
  gg$issue <- sapply(gg$vertex.names, function(i){
    s <- strsplit(i, split = "_")[[1]]
    return(paste0(s[2:length(s)], collapse = "_"))
  })
  gg$number <- sapply(gg$vertex.names, function(i){
    s <- strsplit(i, split = "_")[[1]][1]
    return(as.numeric(gsub("X", "", s)))
  })
  return(gg)
}

gg_overall <- net_prep_plot(pundits_net_withparty)
gg_noparty <- net_prep_plot(pundits_net)
gg_symbolic <- net_prep_plot(pundits_net_symbolic)
gg_operational <- net_prep_plot(pundits_net_operational)
gg_nonpolicy <- net_prep_plot(pundits_net_nonpolicy)

net_plot <- function(gg, issuesub = "Overall", reftab = topic_refdf){
  plot <- 
    gg %>%
    left_join(reftab, by = c("issue" = "topic")) %>%
    ggplot(aes(x = x, y = y, xend = xend, 
               yend = yend,
               label = number))+
    geom_edges(aes(alpha = abs(weight), 
                   lty= factor(issue_match)))+
    geom_text(aes(col = formal_names))+
    scale_alpha_continuous(name = "Tie Strength")+
    scale_color_discrete(name = "Concept")+
    scale_linetype_manual(name = "Tie Type",
                          breaks = c(0,1),
                          values = c(1,2),
                          labels = c("Between Concepts","Within Concept"))+
    labs(title= paste0("Aggregate Concept-Dimension Network: ", issuesub),
         subtitle = "Numbers represent nth dimension for given concept",
         x = "",
         y = "")+
    theme_jg()+
    theme(plot.title = element_text(face = "bold", size = 24),
          plot.subtitle = element_text(size = 20),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          axis.ticks = element_blank(),
          axis.text = element_blank())
  return(plot)
}

plot_overall <- net_plot(gg = gg_overall, issuesub = "All Concepts")
plot_overall_noparty <- net_plot(gg = gg_noparty, issuesub = "All Concepts\nExcept Democrat and Republican")
plot_symbolic <- net_plot(gg = gg_symbolic, issuesub = "Symbolic Concepts")
plot_operational <- net_plot(gg = gg_operational, issuesub = "Operational Concepts")
plot_nonpolicy <- net_plot(gg = gg_nonpolicy, issuesub = "All Non-Policy Concepts")

ggsave(plot_overall, file = paste0("../figures/netfig_ovr.png"), width = 15, height = 9)
ggsave(plot_overall_noparty, file = paste0("../figures/netfig_ovr_noparty.png"), width = 10, height = 9)
ggsave(plot_symbolic, file = paste0("../figures/netfig_symb.png"), width = 10, height = 9)
ggsave(plot_operational, file = paste0("../figures/netfig_oper.png"), width = 10, height = 9)
ggsave(plot_nonpolicy, file = paste0("~../figures/netfig_nonpolicy.png"), width = 10, height = 9)


g_ovr <- igraph::graph_from_adjacency_matrix(pundits_net_withparty$sample$graph,
                                                       weighted = TRUE,
                                                       mode = "undirected")
g_ovr_noparty <- igraph::graph_from_adjacency_matrix(pundits_net$sample$graph,
                                             weighted = TRUE,
                                             mode = "undirected")
g_symb <- igraph::graph_from_adjacency_matrix(pundits_net_symbolic$sample$graph,
                                              weighted = TRUE,
                                              mode = "undirected")

g_oper <- igraph::graph_from_adjacency_matrix(pundits_net_operational$sample$graph,
                                              weighted = TRUE,
                                              mode = "undirected")

g_np <- igraph::graph_from_adjacency_matrix(pundits_net_nonpolicy$sample$graph,
                                            weighted = TRUE,
                                            mode = "undirected")


make_graph_df <- function(graph){
  V(graph)$issue <- sapply(V(graph)$name, function(v){
    s <- strsplit(v, split = "_")[[1]]
    return(paste0(s[2:length(s)], collapse = "_"))
  })
  V(graph)$degree<-igraph::degree(graph,mode="all")
  
  return(as_long_data_frame(graph) %>%
           mutate(issue_match = as.numeric(from_issue == to_issue)))
}

graph_df_ovr <- make_graph_df(g_ovr)
graph_df_ovr_noparty <- make_graph_df(g_ovr_noparty)
graph_df_symb <- make_graph_df(g_symb)
graph_df_oper <- make_graph_df(g_oper)
graph_df_np <- make_graph_df(g_np)

density_ovr <- igraph::graph.density(g_ovr)
density_ovr_noparty <- igraph::graph.density(g_ovr_noparty)
density_symb <- igraph::graph.density(g_symb)
density_oper <- igraph::graph.density(g_oper)
density_np <- igraph::graph.density(g_np)

diameter_ovr <- igraph::diameter(g_ovr, weights = abs(E(g_ovr)$weight))
diameter_ovr_noparty <- igraph::diameter(g_ovr_noparty, weights = abs(E(g_ovr_noparty)$weight))
diameter_symb <- igraph::diameter(g_symb, weights = abs(E(g_symb)$weight))
diameter_oper <- igraph::diameter(g_oper, weights = abs(E(g_oper)$weight))
diameter_np <- igraph::diameter(g_np, weights = abs(E(g_np)$weight))

V(g_ovr)$degree <- igraph::degree(g_ovr,mode="all")
V(g_ovr_noparty)$degree <- igraph::degree(g_ovr_noparty,mode="all")
V(g_symb)$degree <- igraph::degree(g_symb,mode="all")
V(g_oper)$degree <- igraph::degree(g_oper,mode="all")
V(g_np)$degree <- igraph::degree(g_np,mode="all")

make_gback <- function(graph){
  gdat <- igraph::as_data_frame(graph,what = "both")
  
  gdat$edges$from_issue <- sapply(gdat$edges$from, function(f){
    s <- strsplit(f, split = "_")[[1]]
    return(paste0(s[2:length(s)], collapse = "_"))
  })
  gdat$edges$to_issue <- sapply(gdat$edges$to, function(f){
    s <- strsplit(f, split = "_")[[1]]
    return(paste0(s[2:length(s)], collapse = "_"))
  })
  gdat$edges$issue_match <- as.numeric(gdat$edges$from_issue == gdat$edges$to_issue)
  gdat$edges <- gdat$edges %>%
    left_join(gdat$vertices %>% dplyr::select(name, degree),
              by = c("from" = "name"))
  
  gback <- igraph::graph_from_data_frame(gdat$edges)
}

gback_ovr <- make_gback(g_ovr)
gback_ovr_noparty <- make_gback(g_ovr_noparty)
gback_symb <- make_gback(g_symb)
gback_oper <- make_gback(g_oper)
gback_np <- make_gback(g_np)

make_strength_tab <- function(net,refdf = topic_refdf){
  
  g <- igraph::graph_from_adjacency_matrix(net$sample$graph,
                                           weighted = TRUE,
                                           mode = "undirected")
  
  V(g)$issue <- sapply(V(g)$name, function(v){
    s <- strsplit(v, split = "_")[[1]]
    return(paste0(s[2:length(s)], collapse = "_"))
  })
  V(g)$degree<-igraph::degree(g,mode="all")
  
  graph_df <- as_long_data_frame(g) %>%
    mutate(issue_match = as.numeric(from_issue == to_issue)) 
  
  strength_df <- data.frame(node = as.character(sapply(sort(unique(c(graph_df$from_issue, graph_df$to_issue))),
                                                       function(x){
                                                         paste0("X", 1:10, "_", x)
                                                       })))
  strength_df$strength <- sapply(strength_df$node, function(x){
    graph_df %>%
      filter(from_name == x | to_name == x) %>%
      summarise(strength = sum(abs(weight))) %>%
      pull(strength)
  })
  strength_df$dimension <- with(strength_df, readr::parse_number(node))
  strength_df$topic <- with(strength_df, gsub(paste0("X[0-9]_|X10_"), "", node))
  
  return(strength_df %>%
           dplyr::select(topic, dimension, strength) %>%
           left_join(refdf, by = "topic") %>%
           dplyr::select(formal_names, dimension, strength))
}

strengthtab_ovr <- make_strength_tab(net = pundits_net_withparty)
strengthtab_ovr_noparty <- make_strength_tab(net = pundits_net)
strengthtab_symb <- make_strength_tab(net = pundits_net_symbolic)
strengthtab_oper <- make_strength_tab(net = pundits_net_operational)
strengthtab_np <- make_strength_tab(net = pundits_net_nonpolicy)

noderefs <- data.frame(node = as.character(sapply(topic_refdf$topic,
                                                  function(x){
                                                    paste0("X", 1:10, "_", x)
                                                  })),
                       topic = rep(topic_refdf$topic, each = 10),
                       dimension = rep(1:10, 22))

make_n_other_tab <- function(net, graph_df, strengthtab, topicref= topic_refdf){
  
  metaref <- strengthtab %>% left_join(topicref, by = "formal_names") %>%
    mutate(node = paste0("X", dimension, "_", topic))
  
  nodewise_tab <- bind_rows(lapply(metaref$node, function(x){
    net$sampleTable %>%
      left_join(graph_df, 
                by = c("node1" = "from_name", "node2" = "to_name")) %>%
      mutate(weight = ifelse(is.na(weight), 0, weight)) %>%
      rename(source = node1, variable = node2) %>%
      filter(abs(weight) > 0) %>%
      filter(source == x | variable == x) %>%
      summarise(within_issue_ties = sum(as.numeric(from_issue == to_issue)),
                between_issue_ties = sum(as.numeric(!from_issue == to_issue)))
  }))
  
  ret <- bind_cols(metaref, nodewise_tab)
  
  return(ret %>%
           dplyr::select(formal_names, dimension, strength, within_issue_ties, between_issue_ties) %>%
           rename(Concept = formal_names,
                  Dimension = dimension,
                  Strength_Centrality = strength,
                  Within_Issue_Ties = within_issue_ties,
                  Between_Issue_Ties = between_issue_ties))
}

n_other_tab_ovr <- make_n_other_tab(net = pundits_net_withparty, graph_df = graph_df_ovr, 
                                    strengthtab = strengthtab_ovr)
n_other_tab_ovr_noparty <- make_n_other_tab(net = pundits_net, 
                                              graph_df = graph_df_ovr_noparty, 
                                              strengthtab = strengthtab_ovr_noparty)
n_other_tab_symb <- make_n_other_tab(net = pundits_net_symbolic, graph_df = graph_df_symb,
                                     strengthtab = strengthtab_symb)
n_other_tab_oper <- make_n_other_tab(net = pundits_net_operational, graph_df = graph_df_oper,
                                     strengthtab = strengthtab_oper)
n_other_tab_np <- make_n_other_tab(net = pundits_net_nonpolicy, graph_df = graph_df_np,
                                   strengthtab = strengthtab_np)


# table F.1
n_other_tab_ovr %>%
  mutate(Strength_Centrality = round(Strength_Centrality, 3)) %>%
  arrange(desc(Between_Issue_Ties), desc(Strength_Centrality)) %>%
  dplyr::select(Concept, Dimension, Strength_Centrality, Within_Issue_Ties,Between_Issue_Ties ) %>%
  slice(1:20) %>%
  write.csv(file= "../output/centrality_tables/nodewise_other_tab_ovr.csv") # this is table F.1

# other strength centrality tables
n_other_tab_ovr_noparty %>%
  mutate(Strength_Centrality = round(Strength_Centrality, 3)) %>%
  arrange(desc(Between_Issue_Ties), desc(Strength_Centrality)) %>%
  dplyr::select(Concept, Dimension, Strength_Centrality, Within_Issue_Ties,Between_Issue_Ties ) %>%
  slice(1:20) %>%
  write.csv(file= "../output/centrality_tables/nodewise_other_tab_ovr_noparty.csv")

n_other_tab_symb %>%
  mutate(Strength_Centrality = round(Strength_Centrality, 3)) %>%
  arrange(desc(Between_Issue_Ties), desc(Strength_Centrality)) %>%
  dplyr::select(Concept, Dimension, Strength_Centrality, Within_Issue_Ties,Between_Issue_Ties ) %>%
  slice(1:20) %>%
  write.csv(file= "../output/centrality_tables/nodewise_other_tab_symb.csv")

n_other_tab_oper %>%
  mutate(Strength_Centrality = round(Strength_Centrality, 3)) %>%
  arrange(desc(Between_Issue_Ties), desc(Strength_Centrality)) %>%
  dplyr::select(Concept, Dimension, Strength_Centrality, Within_Issue_Ties,Between_Issue_Ties ) %>%
  slice(1:20) %>%
  write.csv(file= "../output/centrality_tables/nodewise_other_tab_oper.csv")

n_other_tab_np %>%
  mutate(Strength_Centrality = round(Strength_Centrality, 3)) %>%
  arrange(desc(Between_Issue_Ties), desc(Strength_Centrality)) %>%
  dplyr::select(Concept, Dimension, Strength_Centrality, Within_Issue_Ties,Between_Issue_Ties ) %>%
  slice(1:20) %>%
  write.csv(file= "../output/centrality_tables/nodewise_other_tab_np.csv")



n_other_tab_ovr %>%
  group_by(Concept) %>%
  summarise(Between_Issue_Ties = sum(Between_Issue_Ties)) %>%
  arrange(desc(Between_Issue_Ties)) %>%
  write.csv(file= "../output/centrality_tables/topicwise_other_tab_ovr.csv")

n_other_tab_ovr_noparty %>%
  group_by(Concept) %>%
  summarise(Between_Issue_Ties = sum(Between_Issue_Ties)) %>%
  arrange(desc(Between_Issue_Ties)) %>%
  write.csv(file= "../output/centrality_tables/topicwise_other_tab_ovr_noparty.csv")

n_other_tab_ovr <- readr::read_csv("../output/centrality_tables/topicwise_other_tab_ovr_noparty.csv")

concept_ties <- 
  n_other_tab_ovr %>%
 ggplot(aes(x = fct_rev(fct_inorder(Concept)),
            y = Between_Issue_Ties))+
  geom_col()+
  coord_flip()+
  labs(x = "Concept",
       y = "Between-Concept Ties",
       title = "Ties Between Concepts",
       caption = "Network includes ten dimensions per concept")+
  theme_jg()
ggsave(concept_ties, file = "../figures/between_topic_ties.png", width = 8, height = 6)
  

n_other_tab_symb %>%
  group_by(Concept) %>%
  summarise(Between_Issue_Ties = sum(Between_Issue_Ties)) %>%
  arrange(desc(Between_Issue_Ties))%>%
  write.csv(file= "../output/centrality_tables/topicwise_other_tab_symb.csv")

n_other_tab_oper %>%
  group_by(Concept) %>%
  summarise(Between_Issue_Ties = sum(Between_Issue_Ties)) %>%
  arrange(desc(Between_Issue_Ties))%>%
  write.csv(file= "../output/centrality_tables/topicwise_other_tab_oper.csv")

n_other_tab_np %>%
  group_by(Concept) %>%
  summarise(Between_Issue_Ties = sum(Between_Issue_Ties)) %>%
  arrange(desc(Between_Issue_Ties)) %>%
  write.csv(file= "../output/centrality_tables/topicwise_other_tab_np.csv")